package com.zw.admin.server.dao;

import com.zw.admin.server.commom.TkMapper;
import com.zw.admin.server.model.*;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface DeviceDao extends TkMapper<Device> {

    //NEW
    //@Select("select * from bsn_device t  where name = '${name}'")
    //Device selectWorkOvertime(@Param("name")String name);

    @Select("select * from bsn_device t  where name ='${name}'")
    List<Device> selectWorkOvertime(@Param("name")String name);

    @Select("select * from bsn_device t  where iot_id = '${iotId}'")
    Device selectByIotId(@Param("iotId")String iotId);

    @Select("select * from bsn_device t  where name like '%${params.name}%'  ${params.orderBy} limit #{offset}, #{limit}")
    List<Device> listByName(Map<String, Object> params, Integer offset, Integer limit);

    @Select("select * from bsn_device t  where iot_id like '%${params.sn}%'  ${params.orderBy} limit #{offset}, #{limit}")
    List<Device> listBySn(Map<String, Object> params, Integer offset, Integer limit);

    @Select("select * from bsn_device t  where iot_id like '%${params.sn}%' and name like '%${params.name}%'  ${params.orderBy} limit #{offset}, #{limit}")
    List<Device> listByAllParam(Map<String, Object> params, Integer offset, Integer limit);

    @Select("select * from bsn_device t  ${params.orderBy} limit #{offset}, #{limit}")
    List<Device> listAll(Map<String, Object> params, Integer offset, Integer limit);

    //获取所有未启用的设备
    @Select("select * from bsn_device where is_enable = 0")
    List<Device> getUnableDevice();

    //获取所有未启用的设备
    @Select("select * from bsn_device where is_enable = 1")
    List<Device> getEnableDevice();

    @Select("select count(id) from bsn_device")
    int count();

    @Select("insert into data_one_day_device (iot_type, insert_time, Data_ENUM_SUM, Data_LUX_SUM, Data_MOISTURE_SUM,\n" +
            "                                 Data_TEMPERATURE_SUM, Data_ELECTRICITY_SUM, Data_POWERS_SUM, Data_QUANTITY_SUM,\n" +
            "                                 Data_VOLTAGE_SUM)\n" +
            "\n" +
            "    (select iot_type,\n" +
            "           now()             insert_time,\n" +
            "           sum(hf.Data_ENUM_1)        Data_ENUM_SUM,\n" +
            "           sum(hf.Data_LUX_1)         Data_LUX_SUM,\n" +
            "           sum(hf.Data_MOISTURE_1)    Data_MOISTURE_sum,\n" +
            "           sum(hf.data_temperature_1) data_temperature_sum,\n" +
            "           sum(hf.Data_ELECTRICITY_1) Data_ELECTRICITY_sum,\n" +
            "           sum(hf.Data_POWERS_1)      Data_POWERS_sum,\n" +
            "           sum(hf.Data_QUANTITY_1)    Data_QUANTITY_sum,\n" +
            "           sum(hf.Data_VOLTAGE_1)     Data_VOLTAGE_sum\n" +
            "    from data_half_hour_device hf\n" +
            "    where to_days(insert_time) = to_days(now())\n" +
            "    group by iot_type);")
    void getOneDayData();

}
